package com.summer.data.jql.builder;

import com.summer.data.Jsql;
import com.summer.data.dialect.Dialect;
import com.summer.data.jql.Context;
import com.summer.data.jql.JsqlAble;
import com.summer.data.jql.behavior.GroupAble;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class GroupBuilder implements JsqlAble {

    List<GroupAble> groupElemList = new ArrayList<>();

    Context ctx;

    public GroupBuilder(Context ctx) {
        this.ctx = ctx;
    }

    public void group(GroupAble... exps) {
        Collections.addAll(groupElemList, exps);
    }

    @Override
    public Jsql buildJsql(Dialect dialect) {
        Jsql jsql = new Jsql();

        for (int i=0; i<groupElemList.size(); i++) {
            GroupAble groupElem = groupElemList.get(i);
            if (i==0) {
                jsql.append(" GROUP BY ");
            } else {
                jsql.append(", ");
            }

            Jsql expJsql = groupElem.toGroupSql(ctx, dialect);
            jsql.append(expJsql);
        }
        return jsql;
    }

}
